package com.ssy.wireless.api.controller;

import com.ssy.wireless.api.service.*;
import com.ssy.wireless.api.vo.*;
import com.ssy.wireless.entity.ApDeviceRecover;
import com.ssy.wireless.entity.LocalResource;
import com.ssy.wireless.entity.MessageAuth;
import com.ssy.wireless.entity.UserAuth;
import com.ssy.wireless.util.DateUtils;
import com.ssy.wireless.util.ReqVo;
import com.ssy.wireless.util.RespVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.Assert;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import static com.ssy.wireless.util.RespVo.success;

/**
 * Created by cofey on 16/11/24.
 */
@Api(description = "外部接口")
@RestController
@RequestMapping("/api")
@CrossOrigin({"*"})
public class ApiController {

    @Autowired
    private ApiApDeviceFaultService faultService;
    @Autowired
    private ApiApDeviceRecoverService recoverService;
    @Autowired
    private ApiApDeviceTrafficService trafficService;
    @Autowired
    private ApiLocalResourceService localResourceService;
    @Autowired
    private ApiMessageAuthService messageAuthService;
    @Autowired
    private ApiUserAuthService userAuthService;

    /**
     * 用户认证记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("用户认证记录接口")
    @PostMapping("/user/auth")
    public RespVo<String> userAuth(
            @ApiParam("请求参数")
            @RequestBody ReqVo<UserAuthVo> reqVo) {
        Assert.notNull(reqVo, "用户认证请求参数不能为空");
        UserAuthVo vo = reqVo.getParams();
        Assert.notNull(vo, "用户认证请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        Assert.hasLength(vo.getMobile(), "用户账号不能为空");
        UserAuth userAuth = new UserAuth();
        userAuth.setApIndex(vo.getApIndex());
        userAuth.setMobile(vo.getMobile());
        userAuth.setUserName(vo.getUserName());
        userAuth.setIp(vo.getIp());
        userAuth.setMac(vo.getMac());
        userAuthService.insert(userAuth);
        return success();
    }

    /**
     * 短信认证记录请求接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("短信认证记录请求接口")
    @PostMapping("/message/auth/request")
    public RespVo<String> msgAuthRequset(
            @ApiParam("请求参数")
            @RequestBody ReqVo<MessageAuthVo> reqVo) {
        Assert.notNull(reqVo, "短信认证请求参数不能为空");
        MessageAuthVo vo = reqVo.getParams();
        Assert.notNull(vo, "短信认证请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        Assert.hasLength(vo.getMobile(), "用户账号不能为空");
        MessageAuth messageAuth = new MessageAuth();
        messageAuth.setApIndex(vo.getApIndex());
        messageAuth.setMobile(vo.getMobile());
        messageAuth.setUserName(vo.getUserName());
        messageAuth.setIp(vo.getIp());
        messageAuth.setMac(vo.getMac());
        messageAuthService.insertRequestInfo(messageAuth);
        return success();
    }

    /**
     * 短信发送记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("短信发送记录接口")
    @PostMapping("/message/auth/send")
    public RespVo<String> msgAuthSend(
            @ApiParam("请求参数")
            @RequestBody ReqVo<MessageAuthVo> reqVo) {
        Assert.notNull(reqVo, "短信发送请求参数不能为空");
        MessageAuthVo vo = reqVo.getParams();
        Assert.notNull(vo, "短信发送请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        Assert.hasLength(vo.getMobile(), "用户账号不能为空");
        MessageAuth messageAuth = new MessageAuth();
        messageAuth.setApIndex(vo.getApIndex());
        messageAuth.setMobile(vo.getMobile());
        messageAuthService.updateSendInfo(messageAuth);
        return success();
    }

    /**
     * 用户登录记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("用户登录记录接口")
    @PostMapping("/message/auth/login")
    public RespVo<String> msgAuthLogin(
            @ApiParam("请求参数")
            @RequestBody ReqVo<MessageAuthVo> reqVo) {
        Assert.notNull(reqVo, "用户登录请求参数不能为空");
        MessageAuthVo vo = reqVo.getParams();
        Assert.notNull(vo, "用户登录请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        Assert.hasLength(vo.getMobile(), "用户账号不能为空");
        MessageAuth messageAuth = new MessageAuth();
        messageAuth.setApIndex(vo.getApIndex());
        messageAuth.setMobile(vo.getMobile());
        messageAuthService.updateLoginInfo(messageAuth);
        return success();
    }

    /**
     * 用户下线记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("用户下线记录接口")
    @PostMapping("/message/auth/user/down")
    public RespVo<String> msgAuthUserDown(
            @ApiParam("请求参数")
            @RequestBody ReqVo<MessageAuthVo> reqVo) {
        Assert.notNull(reqVo, "用户下线请求参数不能为空");
        MessageAuthVo vo = reqVo.getParams();
        Assert.notNull(vo, "用户下线请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        Assert.hasLength(vo.getMobile(), "用户账号不能为空");
        MessageAuth messageAuth = new MessageAuth();
        messageAuth.setApIndex(vo.getApIndex());
        messageAuth.setMobile(vo.getMobile());
        messageAuthService.updateDownInfo(messageAuth);
        return success();
    }

    /**
     * 本地化资源记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("本地化资源记录接口")
    @PostMapping("/local/resource")
    public RespVo<String> localResource(
            @ApiParam("请求参数")
            @RequestBody ReqVo<LocalResourceVo> reqVo) {
        Assert.notNull(reqVo, "本地资源请求参数不能为空");
        LocalResourceVo vo = reqVo.getParams();
        Assert.notNull(vo, "本地资源请求参数不能为空");
        Assert.hasLength(vo.getEntName(), "政企名称不能为空");
        Assert.notNull(vo.getType(), "资源类型不能为空");
        Assert.hasLength(vo.getResName(), "资源名称不能为空");
        LocalResource localResource = new LocalResource();
        localResource.setImgName(vo.getResName());
        localResource.setType(vo.getType());
        localResource.setIntroduce(vo.getIntroduce());
        localResource.setStorePath(vo.getStorePath());
        localResource.setAuditor(vo.getAuditor());
        localResource.setEntName(vo.getEntName());
        try {
            localResource.setAuditTime(StringUtils.isEmpty(vo.getAuditTime()) ? null : DateUtils.parseYMDHMS(vo.getAuditTime()));
        } catch (Exception e) {
            throw new IllegalArgumentException("不是有效的时间格式,格式必须是:2016-11-30 10:10:10");
        }
        localResourceService.insert(localResource);
        return success();
    }

    /**
     * AP设备流量记录接口
     *
     * @param reqVo
     * @return
     */
    @ApiOperation("AP设备流量记录接口")
    @PostMapping("/ap/device/traffic")
    public RespVo<String> apDeviceTraffic(
            @ApiParam("请求参数")
            @RequestBody ReqVo<ApDeviceTrafficVo> reqVo) {
        Assert.notNull(reqVo, "AP设备流量请求参数不能为空");
        ApDeviceTrafficVo vo = reqVo.getParams();
        Assert.notNull(vo, "AP设备流量请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        double upTraffic = 0, downTraffic = 0;
        try {
            upTraffic = Double.valueOf(vo.getUpTraffic());
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("上行流量值不是数字");
        }
        try {
            downTraffic = Double.valueOf(vo.getDownTraffic());
        } catch (NumberFormatException nfe) {
            throw new IllegalArgumentException("下行流量值不是数字");
        }
        trafficService.insertTraffic(vo.getApIndex(), upTraffic, downTraffic);
        return success();
    }

    /**
     * AP设备故障接口
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/ap/device/fault")
    public RespVo<String> apDeviceFault(@RequestBody ReqVo<ApDeviceFaultVo> reqVo) {
        Assert.notNull(reqVo, "设备故障记录请求参数不能为空");
        ApDeviceFaultVo vo = reqVo.getParams();
        Assert.notNull(vo, "设备故障记录请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        faultService.insert(vo.getApIndex());
        return success();
    }

    /**
     * AP设备故障维修记录接口
     *
     * @param reqVo
     * @return
     */
    @PostMapping("/ap/device/recover")
    public RespVo<String> apDeviceRecover(@RequestBody ReqVo<ApDeviceRecoverVo> reqVo) {
        Assert.notNull(reqVo, "设备故障维修记录请求参数不能为空");
        ApDeviceRecoverVo vo = reqVo.getParams();
        Assert.notNull(vo, "设备故障维修记录请求参数不能为空");
        Assert.hasLength(vo.getApIndex(), "AP索引不能为空");
        ApDeviceRecover apDeviceRecover = new ApDeviceRecover();
        apDeviceRecover.setAffirm(vo.getAffirm());
        apDeviceRecover.setOverhaul(vo.getOverhaul());
        apDeviceRecover.setApIndex(vo.getApIndex());
        recoverService.insert(apDeviceRecover);
        return success();
    }
}
